package com.xci.platform.auth.controller;

import com.xci.platform.auth.entity.LogEntity;
import com.xci.platform.auth.service.LogService;
import com.xci.platform.annotation.Authorize;
import com.xci.platform.core.BoolMessage;
import com.xci.platform.core.JsonGrid;
import com.xci.platform.helper.ExcelHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 系统日志控制器
 */
@Authorize
@Slf4j
@Controller
@RequestMapping("/auth/log")
public class LogController extends AuthBaseController {
    /**
     * 日志服务
     */
    @Resource
    protected LogService logService;

    //region 页面视图

    /**
     * 日志首页
     */
    @GetMapping
    @Authorize(code = "SystemLog")
    public String index() {
        return "auth/log/index";
    }

    /**
     * 日志详细页面
     */
    @GetMapping("/details")
    public String details(String id, ModelMap map) {
        LogEntity entity = logService.queryById(id);
        if (entity == null) return notFound();
        map.put("entity", entity);
        return "auth/log/details";
    }

    //endregion

    //region 数据处理

    /**
     * 日志表格数据
     */
    @ResponseBody
    @GetMapping("/grid")
    @Authorize(code = "SystemLog.Search")
    public JsonGrid grid(@RequestParam Map params) {
        return BoolMessage.grid(logService.queryPage(params));
    }

    /**
     * 导出日志
     */
    @GetMapping("/export")
    @Authorize(code = "SystemLog.Export")
    public void export(@RequestParam Map params, HttpServletResponse response) throws IOException {
        List<LogEntity> list = logService.export(params);
        ExcelHelper.exportWeb(response, list, LogEntity.class, "系统日志");
    }

    //endregion
}
